<?php

declare(strict_types=1);

namespace app\admin\controller;

use app\common\builder\TableBuilder;
use app\common\builder\TableFront;
use think\App;
use think\facade\View;
use app\common\model\ProductCate;
use think\response\Json;

use app\common\facade\Builder;

class Product extends Base
{

    //主键，默认为id
    protected $pk = 'id';

    //主模型
    protected $model = '\app\common\model\Product';

    //验证器
    //protected $validate = 'Product';

    //关联模型
    protected $hasOne = ['cate1', 'cate2'];

    //protected $append = ['examineStatusText'];

    public function index()
    {
        return $this->setTable();
    }
    public function setTable()
    {
        //构建table
        return Builder::Table()
            ->addColumn(['type' => 'checkbox', 'fixed' => 'left', 'field' => '', 'title' => '全选'])
            ->addColumn(['field' => 'id', 'sort' => true, 'title' => 'ID', 'width' => 80, 'fixed' => 'left'])
           
            ->addColumn(['field' => 'title', 'title' => '商品名称', 'width' => 200, 'fixed' => 'left'])
            ->addColumn(['field' => 'icon', 'title' => '缩略图', 'width' => 90, 'templet' => '#iconTpl', 'fixed' => 'left'])
            ->addColumn([
                'title'   => '商品分类', 'width' => 200,
                'templet' => '<div>{{d.cate1.title}} - {{d.cate2.title}}</div>'
            ])
            ->addColumn(['field' => 'price', 'title' => '原价'])
            ->addColumn(['field' => 'vip_price', 'title' => 'VIP价格'])
            ->addColumn(['field' => 'freight', 'title' => '运费'])
            ->addColumn(['field' => 'stock', 'title' => '库存'])
            ->addColumn(['field' => 'create_time', 'title' => '添加时间', 'width' => 150])
            ->addColumn(['field' => 'update_time', 'title' => '更新时间', 'width' => 150])
            ->addColumn(['field' => 'sort', 'title' => '排序', 'width' => 120, 'templet' => '#sort_sort'])
            // ->addColumn(['field' => 'is_full', 'title' => '是否参与全场满减', 'templet' => '#is_full', 'width' => 120, 'unresize' => true])
            // ->addColumn(['field' => 'is_coupon', 'title' => '是否使用优惠券', 'templet' => '#is_full', 'width' => 120, 'unresize' => true])
            ->addColumn(['field' => 'status', 'title' => '上架状态', 'templet' => '#status', 'width' => 120, 'unresize' => true])
            ->addColumn(['field' => 'pick', 'title' => '是否开启推荐', 'templet' => '#pick', 'width' => 120, 'unresize' => true])
            //       

            ->addColumn(['fixed' => 'right', 'align' => 'center', 'toolbar' => '#barDemo', 'title' => '操作', 'width' => 150])
            ->addRightButtons(['edit' => ['size' => '1000,1000'], 'delete'])
            ->addTopButtons(['add' => ['size' => '1000,1000'], 'deleteAll'])
            ->addSwitchTool([
                ['name' => 'status', 'pk' => 'id', 'text' => '上架|下架'],
                ['name' => 'pick', 'pk' => 'id', 'text' => '开启|关闭'],
                // ['name' => 'is_full', 'pk' => 'id', 'text' => '参与|不参与'],
                // ['name' => 'is_coupon', 'pk' => 'id', 'text' => '参与|不参与'],
            ])
            ->addSearch([['title', '输入商品名称/描述', 'input', '', '', '', 'like'], ['cate_1', '商品专区', 'select', ProductCate::where(['pid' => 0, 'is_del' => 0])->column('title', 'id')]])
            ->addSortTool([['name' => 'sort', 'pk' => 'id', 'url' => url('update')->build()]])
            
            ->fetch();
    }

    // public function setSearch($action)
    // {
    //     $tableFront = TableFront::getInstance();
    //     $tableFront->setSearch(['business_id', '商家名称', 'input', '', '\app\common\model\Business', 'title,id'])
    //         ->setSearch(['title', '输入商品名称/描述', 'input', '', '', '', 'like'])
    //         ->setSearch(['cate_1', '商品专区', 'select', ProductCate::where(['pid' => 0, 'is_del' => 0])->column('title', 'id')]);
    //     if ($action == '审核') {
    //         $tableFront->setSearch(['examine_status', '审核状态', 'select', [1 => '待审核', 2 => '已通过', 3 => '已驳回']]);
    //     }
    //     return $tableFront;
    // }


    public function add($tpl = 'product/edit')
    {
        View::assign('cate', ProductCate::getCate());
        return parent::add($tpl);
    }

    public function edit($id = '', $tpl = 'product/edit', $info = '')
    {
        $info = $this->model::find($id);
        View::assign('cate', ProductCate::getCate());
        
        //View::assign('service', \app\common\model\ProductService::where('is_del', 0)->select());
        return parent::edit($id, $tpl, $info);
    }

    public function getProduct()
    {
        try {
            $param = $this->request->get();
            $list = $this->model::whereLike('title', '%' . $param['val'] . '%')
                ->with(['business'])
                ->field('id,title,icon,business_id,price,vip_price')->select();
            return json(['code' => 0, 'data' => $list]);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }

    public function saveProduct(): Json
    {
        return json($this->model::saveProduct($this->request->post())); // TODO: Change the autogenerated stub
    }

    public function examineSave()
    {
        return json($this->model::examineSave($this->request->post(), $this->admin));
    }

    public function examineDelete()
    {
        try {
            $id = $this->request->post($this->pk);
            $model = $this->model::where($this->pk, $id)->find();
            $model->examine_del = 1;
            $model->save();
            \app\common\model\Logs::adds('删除【审核商品】记录ID【' . $id . '】');
            return json(['code' => 1, 'msg' => '已删除']);
        } catch (\Exception $e) {
            return json(['code' => -1, 'msg' => $e->getMessage()]);
        }
    }
}
